fix: cache worker IPC state for prealloc thread#336
Open
shipiyouniao wants to merge 1 commit into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Prevents a known deadlock during single-GPU startup by conditionally disabling the C++ background preallocation thread when world_size == 1.
Changes:
- Gate
start_prealloc_thread()behindworld_size > 1 - Add inline explanation describing the deadlock scenario
- Add an info log when preallocation is skipped for single-GPU runs
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
bcf4d13 to
10682ec
Compare
10682ec to
a48f046
Compare
a481bb1 to
7de21c7
Compare
7de21c7 to
f51e3ab
Compare
Avoid calling the Python worker IPC callback from the C++ prealloc thread. On single-GPU startup that callback can block on the GIL before the first page mapping call, which hangs background preallocation and keeps the server from becoming ready.
f51e3ab to
6f20be5
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fix single-GPU startup hangs by caching the worker-IPC decision for the C++ background prealloc thread instead of calling back into Python from that thread.
Related issue: #334
Root cause
On world_size=1, the background prealloc thread enters PageAllocator::map_pages() during startup and evaluates should_use_worker_ipc().
That check was implemented as a Python callback. The prealloc thread therefore tried to re-enter Python while the main Python thread was still inside extension code and holding the GIL during startup. The background thread stalled before the first KV page mapping call, preallocation never completed, and the server never became ready.
The previous workaround avoided the hang by skipping the background prealloc thread for world_size=1, but it did not fix the underlying cause.
Changes
Why this approach
This keeps the behavior change narrowly scoped to the background prealloc thread, which is the only place that needs to avoid Python callback re-entry during startup. It restores the intended preallocation behavior on single-GPU startup instead of bypassing it.
Validation
Validated against the single-GPU reproduction described in issue #334:
Observed after the fix:
Mapped 3 pages to KV tensorsPreallocated 3 pages, reserved=3GET /healthreturns 200GET /v1/modelsreturns 200POST /v1/chat/completionsreturns 200Notes
This replaces the earlier workaround commit that skipped background preallocation for world_size=1. The new change fixes the verified deadlock mechanism directly.